package com.allm.ai.common.entity;

import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.Builder;
import org.hibernate.annotations.GenericGenerator;

import java.time.LocalDateTime;

/**
 * Excel导入记录实体
 * 用于跟踪Excel文件导入的状态和统计信息
 * 
 * @author com.enplatform.assistant
 * @since 1.0.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "import_records")
public class ImportRecord {

    /**
     * 主键ID，使用UUID
     */
    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "uuid2")
    @Column(name = "id", length = 36)
    private String id;

    /**
     * 原始文件名
     */
    @Column(name = "file_name", nullable = false, length = 255)
    private String fileName;

    /**
     * 文件大小（字节）
     */
    @Column(name = "file_size")
    private Long fileSize;

    /**
     * 文件MD5哈希值，用于去重
     */
    @Column(name = "file_hash", length = 32)
    private String fileHash;

    /**
     * 导入状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    private ImportStatus status;

    /**
     * 总记录数
     */
    @Column(name = "total_records")
    private Integer totalRecords;

    /**
     * 有效记录数（通过验证的记录）
     */
    @Column(name = "valid_records")
    private Integer validRecords;

    /**
     * 成功处理的记录数（成功生成向量并存储）
     */
    @Column(name = "success_records")
    private Integer successRecords;

    /**
     * 失败记录数
     */
    @Column(name = "failed_records")
    private Integer failedRecords;

    /**
     * 开始处理时间
     */
    @Column(name = "start_time")
    private LocalDateTime startTime;

    /**
     * 结束处理时间
     */
    @Column(name = "end_time")
    private LocalDateTime endTime;

    /**
     * 处理耗时（毫秒）
     */
    @Column(name = "processing_time_ms")
    private Long processingTimeMs;

    /**
     * 错误信息
     */
    @Column(name = "error_message", columnDefinition = "TEXT")
    private String errorMessage;

    /**
     * 向量集合名称
     */
    @Column(name = "vector_collection", length = 100)
    private String vectorCollection;

    /**
     * 向量维度
     */
    @Column(name = "vector_dimension")
    private Integer vectorDimension;

    /**
     * 创建时间
     */
    @Column(name = "created_at", nullable = false)
    private LocalDateTime createdAt;

    /**
     * 更新时间
     */
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    /**
     * 创建者
     */
    @Column(name = "created_by", length = 100)
    private String createdBy;

    /**
     * 扩展元数据，存储JSON格式的额外信息
     */
    @Column(name = "metadata", columnDefinition = "JSON")
    private String metadata;

    /**
     * 处理步骤记录，存储JSON格式的处理过程
     */
    @Column(name = "processing_steps", columnDefinition = "TEXT")
    private String processingSteps;

    /**
     * 自动设置创建时间
     */
    @PrePersist
    protected void onCreate() {
        if (createdAt == null) {
            createdAt = LocalDateTime.now();
        }
        if (updatedAt == null) {
            updatedAt = LocalDateTime.now();
        }
    }

    /**
     * 自动更新修改时间
     */
    @PreUpdate
    protected void onUpdate() {
        updatedAt = LocalDateTime.now();
    }

    /**
     * 计算成功率
     */
    public double getSuccessRate() {
        if (totalRecords == null || totalRecords == 0) {
            return 0.0;
        }
        return successRecords != null ? (double) successRecords / totalRecords * 100 : 0.0;
    }

    /**
     * 判断是否处理完成
     */
    public boolean isCompleted() {
        return status == ImportStatus.SUCCESS || status == ImportStatus.FAILED;
    }

    /**
     * 判断是否处理成功
     */
    public boolean isSuccess() {
        return status == ImportStatus.SUCCESS;
    }

    /**
     * 导入状态枚举
     */
    public enum ImportStatus {
        /**
         * 处理中
         */
        PROCESSING("处理中"),
        
        /**
         * 成功
         */
        SUCCESS("成功"),
        
        /**
         * 失败
         */
        FAILED("失败"),
        
        /**
         * 部分成功
         */
        PARTIAL_SUCCESS("部分成功"),
        
        /**
         * 已取消
         */
        CANCELLED("已取消");

        private final String description;

        ImportStatus(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }
}
